package com.rp.infopub.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rp.common.utils.JsonUtil;
import com.rp.common.utils.RpUtil;
import com.rp.common.vo.PageData;
import com.rp.controller.RpController;
import com.rp.infopub.po.Article;
import com.rp.infopub.po.Channel;
import com.rp.infopub.service.ArticleService;
import com.rp.infopub.service.ChannelService;
import com.rp.system.po.User;

@Controller
@RequestMapping("/rp/infopub/articleMng/")
public class ArticleController extends RpController{

	@Autowired
	private ArticleService articleService;
	@Autowired
	private ChannelService channelService;
	
	@RequestMapping("showArticleGrid")
	public ModelAndView showArticleGrid(){
		model.put(WEBPAGE_TITLE, "文章管理");
		return forward("/rp/infopub/articleMng/articleGrid.jsp");
	}
	
	@RequestMapping("showArticleForm")
	public ModelAndView showArticleForm(){
		model.put(WEBPAGE_TITLE, "文章编辑");
		return forward("/rp/infopub/articleMng/articleForm.jsp");
	}
	
	@ResponseBody
	@RequestMapping("getArticleById")
	public Article getArticleById(String articleId){
		Article article = articleService.getArticleDao().get(articleId);
		
		return article;
	}
	
	@ResponseBody
	@RequestMapping("saveArticle")
	public String saveArticle(String json, String content){
		Map<String, Object> obj = JsonUtil.jsonToMap(json);
		
		Article art = null;
		String id = (String) obj.get("id");
		//如果ID为空，则新建保存，否则更新保存
		if(RpUtil.isNullOrEmpty(id)){
			art = new Article();
			art.setTitle((String)obj.get("title"));
			art.setSummary((String)obj.get("summary"));
			art.setChannelId((String)obj.get("channelId"));
			//设置所属栏目名称
			Channel channel = channelService.getChannelDao().get(art.getChannelId());
			art.setChannelName(channel.getName());
			art.setContent(content);
			art.setCreatorId(User.getCurrentUser().getId());
			art.setCreatorName(User.getCurrentUser().getRealname());
			art.setIp(request.getRemoteAddr());
			art.setEnabled2((String)obj.get("enabled2"));
			art.setImgPath((String)obj.get("imgPath"));
			
			String newId = articleService.saveArticle(art);
			if(newId.isEmpty()){
				return ERROR;
			}
		}else{
			art = articleService.getArticleDao().get(id);
			art.setTitle((String)obj.get("title"));
			art.setSummary((String)obj.get("summary"));
			art.setChannelId((String)obj.get("channelId"));
			//设置所属栏目名称
			Channel channel = channelService.getChannelDao().get(art.getChannelId());
			art.setChannelName(channel.getName());
			art.setContent(content);
			art.setEnabled2((String)obj.get("enabled2"));
			art.setImgPath((String)obj.get("imgPath"));
			articleService.updateArticle(art);
		}
		
		return SUCCESS;
	}
	
	@ResponseBody
	@RequestMapping("saveArticles")
	public String saveArticles(String data){
		List<Article> articles;
		try {
			articles = JsonUtil.jsonToList(data, "com.rp.infopub.po.Article");
			for (Article article : articles) {
				if(article.getId() != null){
					articleService.updateArticle(article);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	@ResponseBody
	@RequestMapping("deleteArticles")
	public String deleteArticles(String data){
		List<Article> articles;
		try {
			articles = JsonUtil.jsonToList(data, "com.rp.infopub.po.Article");
			for (Article article : articles) {
				articleService.deleteArticle(article.getId());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	

	@ResponseBody
	@RequestMapping("getArticles")
	public PageData getArticles(String title, String channelId){
		
		return new PageData(articleService.getArticleDao().getArticlesByPage(pageParam, title, channelId));
	}
	
	@RequestMapping("showArticleDetail")
	public ModelAndView showArticleDetail(String articleId){
		Article article = articleService.getArticleDao().get(articleId);
		//article.setTitle(ESAPI.encoder().encodeForHTML(article.getTitle()));
		//article.setSummary(ESAPI.encoder().encodeForHTML(article.getSummary()));
		model.put("article", article);
		return forward("/rp/infopub/articleMng/articleDetail.jsp");
	}
	
	/**
	 * 显示帮助文档文章
	 * @param articleId
	 * @return
	 */
	@RequestMapping("showHelpArticle")
	public ModelAndView showHelpArticle(String articleId){
		try{
			Article article = articleService.getArticleDao().get(articleId);
			model.put(WEBPAGE_TITLE, "帮助文档："+article.getTitle());
			model.put("article", article);
		}catch (Exception e) {
			e.printStackTrace();
			model.put(WEBPAGE_TITLE, "请求出错");
			model.put("errorMsg", "查询不到对应文章");
			return forward("/rp/common/error.jsp");
		}
		
		return forward("/rp/infopub/articleMng/articleDetail.jsp");
	}
	
	@RequestMapping("tt")
	public String tt(){
		User user = User.getCurrentUser();
		List<GrantedAuthority> gas = (List<GrantedAuthority>) user.getAuthorities();
		for (GrantedAuthority ga : gas) {
			System.out.println(ga.getAuthority());
		}
		return SUCCESS;
	}
	
}
